//
//  PassCodeViewController.m
//  Voxper
//
//  Created by Erphan on 2/10/11.
//  Copyright 2011 Smonte. All rights reserved.
//

#import "PassCodeViewController.h"
#import "VoxperAppDelegate.h"
#import "UIManager.h"
#import "Settings.h"
#import "Constants.h"

@interface PassCodeViewController(Private)
- (void)hideKeyboard;
@end

@implementation PassCodeViewController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
		NSLog(@"PassCode Init: Is main thread? %@", [NSThread isMainThread]?@"YES":@"NO");
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationReceived:) name:@"PasscodeCallback" object:nil];
		app = (VoxperAppDelegate *)[UIApplication sharedApplication].delegate;
		
		activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
		activity.hidesWhenStopped = YES;
		
		UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:activity];
		[self.navigationItem setRightBarButtonItem:barButton animated:YES];
		[barButton release];
    }
    return self;
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[[self navigationItem] setTitle:APP_NAME];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewWillDisappear:(BOOL)animated {
	//[txtCode resignFirstResponder];
	[super viewWillDisappear:animated];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	[activity release];
	[btnEnterVoxper release];
	[txtCode release];
    [super dealloc];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
	[textField resignFirstResponder];
	return NO;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 4) ? NO : YES;
}

- (IBAction)btnEnterVoxper_Clicked:(id)sender {
	NSLog(@"PassCode: Is main thread? %@", [NSThread isMainThread]?@"YES":@"NO");
	[txtCode resignFirstResponder];
	btnEnterVoxper.enabled = NO;
	[activity startAnimating];
	[app.uiManager verifyCode:txtCode.text];
	//[app.uiManager performSelector:@selector(verifyCode:) withObject:txtCode.text afterDelay:3];
	//[btnEnterVoxper setUserInteractionEnabled:NO];
	//[self performSelectorInBackground:@selector(hideKeyboard) withObject:nil];
}
- (IBAction)btnEnterVoxper_TouchDown:(id)sender {
	[txtCode resignFirstResponder];
}

- (void)hideKeyboard {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	[txtCode resignFirstResponder];
	[pool release];
}

- (void)notificationReceived:(NSNotification *)note {
	if ([NSThread isMainThread]) {
		[txtCode resignFirstResponder];
		[activity stopAnimating];
		btnEnterVoxper.enabled = YES;
	}
	
	if ([note object]) {
		NSString *strInfo = (NSString *)[note object];
		if ([strInfo isEqualToString:@"Registered"]) {
			[self dismissModalViewControllerAnimated:YES];
			[app initiateUI];
		} else {
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:APP_NAME message:strInfo delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
			[alert show];
			[alert release];
			[txtCode setUserInteractionEnabled:YES];
		}
		
	} else {
		
	}
}


@end
